<?php

ini_set('memory_limit','16M');
set_time_limit( 60 * 5 );

require_once( '/home/cyberpower678/Peachy/Init.php' );
require_once('/home/cyberpower678/database.inc');

$site = Peachy::newWiki( "cyberbot i" );

$site->set_runpage("User:Cyberbot I/Run/CHU");


$chu_page = $site->initPage( "Wikipedia:Changing username/Simple" );
$chu_page = $chu_page->get_text();

preg_match_all('/\=\=\=\s*(.*)\s*\→\s*(.*)\s*\=\=\=(.*)(?=\=\=\=\s*\S.*\s*\=\=\=(.*)|$)/Us',$chu_page,$m,PREG_SET_ORDER);

foreach( $m as $req ) {
        $request = $req[0];
        $from = ucfirst( preg_replace( '/^(NEW)?\s?/', '', $req[1] ) );
        $to = ucfirst( preg_replace( '/^(CURRENT)?\s?/', '', $req[2] ) );
        $content = $req[3];

        if( empty( $request ) || empty( $content ) || empty( $to ) || empty( $from ) ) {
                die( "Bad API result.\n" );
        }
        
        if (preg_match('/\{\{[oO]n\s?hold/i',$content)) continue;
        if (preg_match('/\{\{CHU.*/i',$content)) continue;
        if (preg_match('/\{\{[cC]lerk\s?note/i',$content)) continue;
        if (preg_match('/\{\{[cC]rat\s?note/i',$content)) continue;
        if (preg_match('/\{\{[dD]one\}\}/i',$content)) continue;
        if (preg_match('/\{\{[nN]ot\s?done\}\}/i',$content)) continue;
        if (preg_match('/\{\{\[\[Image:.*/i',$content)) continue;
        if (preg_match('/SoxBot/i',$content)) continue;
        if (!preg_match('/\{\{renameuser2/i',$content)) continue;

        echo "New request for $to, requested by $from.\n";
        
        $data = array();

        $to_user_info = $site->initUser( $to );
        $data['taken'] = $to_user_info->exists();
        
        $from_user_info = new User( $site, $from );
        if( !$from_user_info->exists() ) continue;
        
        $data['editcount'] = $from_user_info->get_editcount();
        
        if (preg_match('/\[\[Special\:Contributions\/(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\|\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\]\] \(\[\[User talk\:\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\|talk\]\]\) \d\d\:\d\d\, \d\d \w* \d\d\d\d \(UTC\)/', $content, $ir)) {
                $data['ip'] = $ir[1];
        }
        else {
                $data['ip'] = 'no';
        }
        
        if( !$from_user_info->is_blocked() ) {
                $data['nowblocked'] = 'no';
        }
        else {
                $data['nowblocked'] = 'yes';
        }
        
        $data['long'] = (strlen($to) > 40)? 'yes' : 'no';
        
        $data['beta'] = 'beta';
        
        $gui = new GlobalUserInfo( $site, $to );
        
        $data['sul'] = 'no';
        if( $gui->get_exists() ) {
                $data['sul'] = 'yes';
        }
        
        print_r($data);
        
        $template = null;
        $changed = false;
        $errors = array();
        
        if( $data['taken'] ) {
                if ( $data['editcount'] != 0 ) {
                        $errors[] = "Target username exists, and it has edits, deleted edits, and/or log edits";
                }
                else {
                        $errors[] = 'Target username exists, but you can place a request to usurp the username at [[WP:USURP]]';
                }
        }
        
        if ($data['ip'] != 'no') {
                $errors[] = "Request was made by ip " . $data['ip'] . ", instead of the original account";
        }
        
        if ($data['nowblocked'] == "yes") {
                $errors[] = "Old username is currently blocked";
        }
        
        if ($data['long'] == "yes") {
                $errors[] = "New username is longer than 40 characters";
        }

        if ($data['sul'] == "yes") {
                $errors[] = "Target account conflicts with SUL";
        }
        
        if (count($errors) > 0) {
                $toadd = "*{{User:Cyberbot I/CHU|error".$data['beta']."|";
                $toadd .= implode( '; ', $errors );
                $toadd .= ".}} ~~~~\n";
                $changed = true;
        }
        else {
                $toadd = "*{{User:Cyberbot I/CHU|noerror".$data['beta']."}} ~~~~\n";
                $changed = true;
        }
        
        if ($changed == true) {
                $chu_page = $site->initPage("Wikipedia:Changing username/Simple");
                $chu_page_text = $chu_page->get_text();
                
                $append = $content . "\n" . $toadd;
                $append = str_replace($content, $append, $chu_page_text);
                if ($append == '' || empty($append)) { continue; }
                
                echo "\n\n\n\n\n\n\n\n\n\n\n".getTextDiff('unified', $chu_page_text, $append);
                
                $chu_page->edit( $append, "Adding clerk notices (BOT)" );
        }
}

?>

